package com.tvein.web.controller.FileLoader
{
	//flash
	import flash.net.LocalConnection;
	import flash.utils.Timer;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.TimerEvent;
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	//custom
	import com.tvein.web.common.CommonConstants;
	import com.tvein.web.model.ServerProxy;
	import com.tvein.web.model.FilesProxy;
//
	public class preLoad extends SimpleCommand implements ICommand
	{
		private var serverData:ServerProxy;
		private var filesData:FilesProxy;
		private var fileSrc:String;
		
		override public function execute(notification:INotification) : void
		{
			serverData = facade.retrieveProxy( ServerProxy.NAME ) as ServerProxy;
			filesData = facade.retrieveProxy(FilesProxy.NAME) as FilesProxy;
			
			fileSrc = notification.getBody().toString();
			
			var loader:URLLoader = new URLLoader();
			
			loader.load(new URLRequest(serverData.gameServer + fileSrc + "?tmp=" + Math.random()));
			loader.addEventListener(Event.COMPLETE, getFileList);
		}
		
		private function getFileList(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, getFileList);
			
			var fileList:XML = new XML(evt.target.data);
			var localConnection:LocalConnection = new LocalConnection();
			
			var testFile:Boolean;
			
			for (var i:int = 0; i < fileList.file.length(); i++)
			{
				testFile = false;
				
				//检查是否存在
				for (var j:int = 0; j < filesData.filesArray.length; j++)
				{
					if ((fileList.file.@filePath[i] + fileList.file.@fileName[i] + "|" + fileList.file.@fileVersion[i]) == filesData.filesArray[j][0])
					{
						testFile = true;
					}
				}
				
				if (!testFile)
				{
					localConnection.send(CommonConstants.SYSTEM_CHANNEL, "console", ["/fileloader -D", [CommonConstants.WEB_CHANNEL, fileList.file.@filePath[i] + "" + fileList.file.@fileName[i], fileList.file.@fileVersion[i]]]);
				}
			}
			
			var checkTimer:Timer = new Timer(1000);
			checkTimer.addEventListener(TimerEvent.TIMER, function(evt:TimerEvent):void {_fileLoadComplete(evt, fileList);});
			checkTimer.start();
			
			localConnection = null;
		}
		
		private function _fileLoadComplete(evt:TimerEvent, fileList:XML):void
		{
			var numLoad:int = 0;
			
			for (var i:int = 0; i < fileList.file.length(); i++)
			{
				for (var j:int = 0; j < filesData.filesArray.length; j++)
				{
					if ((fileList.file.@filePath[i] + fileList.file.@fileName[i] + "|" + fileList.file.@fileVersion[i]) == filesData.filesArray[j][0])
					{
						numLoad ++; 
					}
				}
			}
			
			if (i == numLoad)
			{
				evt.target.stop();
				evt.target.removeEventListener(TimerEvent.TIMER, _fileLoadComplete);
				sendNotification(CommonConstants.FILELOADER, ["/fileloader -L", fileSrc]);
			}
		}
	}
}